package com.huluobo.a2211mdmdemo.views

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.navigation.NavHostController
import com.blankj.utilcode.util.ToastUtils
import com.huluobo.lib_base.BaseTitle
import com.hyphenate.EMValueCallBack
import com.hyphenate.chat.EMClient
import com.hyphenate.chat.EMGroup
import com.hyphenate.chat.EMGroupManager
import com.hyphenate.chat.EMGroupOptions
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking

/**
 *  Created by LC on 2025/5/10.
 */

@Composable
fun CreateGroupPage(navHostController: NavHostController) {

    //讨论组名字
    var groupName by remember {
        mutableStateOf("")
    }

    //讨论组描述
    var groupDesc by remember {
        mutableStateOf("")
    }

    Column(
        Modifier
            .fillMaxWidth()
            .padding(20.dp), verticalArrangement = Arrangement.spacedBy(20.dp)
    ) {
        BaseTitle(title = "创建讨论组", navHostController = navHostController)
        Spacer(modifier = Modifier.height(10.dp))
        OutlinedTextField(value = groupName, onValueChange = { groupName = it }, Modifier.fillMaxWidth(), label = {
            Text(text = "讨论组名字")
        })
        OutlinedTextField(value = groupDesc, onValueChange = { groupDesc = it }, Modifier.fillMaxWidth(), label = {
            Text(text = "讨论组描述", maxLines = 3)
        })
        Button(onClick = {
            //群设置
            val option = EMGroupOptions()
            //设置最大人数
            option.maxUsers = 100
            //设置入群规则,当前为直接加入群,无需同意
            option.style = EMGroupManager.EMGroupStyle.EMGroupStylePublicOpenJoin
            EMClient.getInstance().groupManager().asyncCreateGroup(
                groupName,
                groupDesc,
                arrayOf(EMClient.getInstance().currentUser),
                "",
                option, object : EMValueCallBack<EMGroup> {
                    override fun onSuccess(value: EMGroup?) {
                        runBlocking(Dispatchers.Main) {
                            ToastUtils.showLong("创建讨论组成功")
                            navHostController.popBackStack()
                        }
                    }

                    override fun onError(error: Int, errorMsg: String?) {
                        ToastUtils.showLong("创建讨论组失败:$error,msg:$errorMsg")
                    }
                }
            )

        }) {
            Text(text = "创建讨论组")
        }
    }
}